package com.lhl.mongodb;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

public class MongoDao {
    private MongoCollection<Document> mongoCollection;

    public MongoDao(MongoDatabase mongoDataBase, String collection){
        this.mongoCollection = mongoDataBase.getCollection(collection);
    }

    public void insert(String json){
        Document document = Document.parse(json);
        mongoCollection.insertOne(document);
    }

    public void insert(Document document){
        mongoCollection.insertOne(document);
    }

    public void updateOne(String queryJson,String modifiersJson){
        Document query = Document.parse(queryJson);
        Document modifiers = new Document();
        modifiers.append("$set", Document.parse(modifiersJson));
        mongoCollection.updateOne(query,modifiers);
    }

    public void updateOne(Document query,Document modifier){
        Document modifiers = new Document();
        modifiers.append("$set", modifier);
        mongoCollection.updateOne(query,modifiers);
    }

    public void updateMany(String queryJson,String modifiersJson){
        Document query = Document.parse(queryJson);
        Document modifiers = new Document();
        modifiers.append("$set", Document.parse(modifiersJson));
        mongoCollection.updateMany(query,modifiers);
    }

    public void updateMany(Document query,Document modifier){
        Document modifiers = new Document();
        modifiers.append("$set", modifier);
        mongoCollection.updateMany(query,modifiers);
    }

    public void saveOrUpdate(String queryJson,String modifierJson){
        Document query = Document.parse(queryJson);
        Document modifiers = new Document();
        modifiers.append("$set", Document.parse(modifierJson));
        mongoCollection.updateOne(query,modifiers,new UpdateOptions().upsert(true));
    }

    public void delete(String json){
        Document document = Document.parse(json);
        FindIterable<Document> docs = mongoCollection.find(document);
        MongoCursor<Document> iterator = docs.iterator();
        while(iterator.hasNext()){
            Document doc = iterator.next();
            mongoCollection.deleteOne(doc);
        }
    }

    public void delete(Document document){
        FindIterable<Document> docs = mongoCollection.find(document);
        MongoCursor<Document> iterator = docs.iterator();
        while(iterator.hasNext()){
            Document doc = iterator.next();
            mongoCollection.deleteOne(doc);
        }

    }

    public FindIterable<Document> findAll(){
        return mongoCollection.find();
    }

    public long count(String json){
        Document document = Document.parse(json);
        return count(document);
    }

    public long count(Document document){
        return mongoCollection.count(document);
    }

    public FindIterable<Document> find(String json){
        Document document = Document.parse(json);
        return mongoCollection.find(document);
    }

    public FindIterable<Document> find(Document document){
        return mongoCollection.find(document);
    }


    public Document findOne(String json){
        Document document = Document.parse(json);
        return findOne(document);
    }

    public Document findOne(Document document){
        FindIterable<Document> docs = mongoCollection.find(document);
        MongoCursor<Document> iterator = docs.iterator();
        while(iterator.hasNext()){
            return iterator.next();
        }
        return null;
    }

    public void replaceOne(String queryJson,String objJson){
        Document query = Document.parse(queryJson);
        Document modifiers = Document.parse(objJson);
        mongoCollection.replaceOne(query,modifiers);
    }
}
